<!DOCTYPE HTML>
<html>
<head>
<meta name="viewport" content="initial-scale=1">
<title>Video Context Loss Test</title>
<style type="text/css">
.nomargin {
  margin: 0px auto;
}
</style>
<script>
var video;

var QueryString = function() {
  // Allows access to query parameters on the URL; e.g., given a URL like:
  //    http://<server>/my.html?test=123&bob=123
  // Parameters can then be accessed via QueryString.test or QueryString.bob.
  var params = {};
  // RegEx to split out values by &.
  var r = /([^&=]+)=?([^&]*)/g;
  // Lambda function for decoding extracted match values. Replaces '+' with
  // space so decodeURIComponent functions properly.
  function d(s) { return decodeURIComponent(s.replace(/\+/g, ' ')); }
  var match;
  while (match = r.exec(window.location.search.substring(1)))
    params[d(match[1])] = d(match[2]);
  return params;
}();

function CrashGpuProcess() {
  // Ensure we only get one crash in case of multiple canplaythrough events.
  video.removeEventListener('canplaythrough', CrashGpuProcess, false);

  // Create a canvas element and webgl context -- without a context, we won't
  // get a webglcontextlost event.
  var canvas = document.createElement('canvas');
  var gl = canvas.getContext('webgl');

  // webglcontextlost may not be invoked unless we actually try to execute a GL
  // command, so use a repeating function to tickle the context.
  var tickle = setInterval(function() { gl.getParameter(gl.VERSION); }, 250);

  // Now wait for the context loss before starting video playback.
  canvas.addEventListener('webglcontextlost', function(e) {
    clearInterval(tickle);

    // Now wait for the video to play until the end, by which point a frame
    // should be visible on screen.
    video.addEventListener('ended', function(e) {
      domAutomationController.send('SUCCESS');
    }, false);
    video.play();
  }, false);

  // Invokes chrome://gpucrash, which triggers a webglcontextlost event above.
  chrome.gpuBenchmarking.crashGpuProcess();
}

function Main() {
  video = document.getElementById('video');

  // Wait until we're sure a frame has been displayed before crashing GPU.
  video.addEventListener('canplaythrough', CrashGpuProcess, false);

  // Add an error listener to avoid timeouts if playback fails.
  video.addEventListener('error', function(e) {
    console.log('Video playback failed: ' + e.code + ', "' + e.message + '"');
    domAutomationController.send('ERROR');
  }, false);

  // We set a poster and preload=metadata to ensure that no frames are decoded
  // before the gpu crash -- otherwise the media pipeline can't guarantee that
  // we'll fall over to the software decoder.
  video.poster = 'single_face.jpg';
  video.preload = 'metadata';

  // src needs to be set after listeners are added.
  video.src = QueryString.src;
}
</script>
</head>
<body onload="Main()">
<div id="container" style="position:absolute; top:0px; left:0px">
<video class="nomargin" id="video" width="240" height="135"></video>
</div>
</body>
</html>
